package com.corona.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.corona.Game;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;

/* compiled from: Network.java */
/* access modifiers changed from: package-private */
/* loaded from: pokewilds.jar:com/pkmngen/game/ServerBroadcast.class */
public class ServerBroadcast extends Action {
    int timeStep = 0;
    public Action.Layer layer = Action.Layer.map_1;

    public ServerBroadcast(final Game game) {
        super(new Object[0]);
        game.server.addListener(new Listener() { // from class: com.corona.game.ServerBroadcast.1
            @Override // com.esotericsoftware.kryonet.Listener
            public void received(final Connection connection, final Object object) {
                Runnable runnable = new Runnable() { // from class: com.corona.game.ServerBroadcast.1.1
                    @Override // java.lang.Runnable
                    public void run() {
                        try {
                            if (object instanceof Network.Login) {
                                Network.Login login = (Network.Login) object;
                                if (!game.players.containsKey(login.playerId)) {
                                    String playerId = login.playerId;
                                    Player player = new Player();
                                    Vector2 startLoc = game.map.edges.get(game.map.rand.nextInt(game.map.edges.size()));
                                    player.position.set(startLoc);
                                    player.spawnLoc.set(startLoc);
                                    player.type = Player.Type.REMOTE;
                                    Objects.requireNonNull(player);
                                    player.network = new Player.Network(player.position);
                                    player.network.connectionId = connection.getID();
                                    player.network.id = playerId;
                                    player.network.number = String.valueOf(game.players.keySet().size());
                                    player.setColor(login.color);
                                    player.network.tiles = game.map.overworldTiles;
                                    player.currPokemon = new Pokemon("machop", 6);
                                    player.pokemon.add(player.currPokemon);
                                    game.players.put(playerId, player);
                                }
                                Player player2 = game.players.get(login.playerId);
                                player2.network.connectionId = connection.getID();
                                connection.sendTCP(new Network.PlayerData(player2));
                                if (game.battles.containsKey(player2.network.id)) {
                                    connection.sendTCP(new Network.BattleData(game.battles.get(player2.network.id).oppPokemon));
                                }
                                if (player2.standingAction == null) {
                                    player2.standingAction = new PlayerStanding(game, player2, false, true);
                                    game.insertAction(player2.standingAction);
                                }
                                Network.MapTiles mapTiles = new Network.MapTiles();
                                Iterator<Vector2> it = player2.network.loadingZone.allPositions().iterator();
                                while (it.hasNext()) {
                                    Vector2 position = it.next();
                                    Tile tile = game.map.overworldTiles.get(position);
                                    if (tile != null) {
                                        mapTiles.tiles.add(new Network.TileData(tile));
                                        Tile tile2 = game.map.interiorTiles.get(game.map.interiorTilesIndex).get(position);
                                        if (tile2 != null) {
                                            mapTiles.tiles.add(new Network.TileData(tile2, game.map.interiorTilesIndex));
                                        }
                                        if (game.map.pokemon.containsKey(position)) {
                                            connection.sendTCP(new Network.OverworldPokemonData(game.map.pokemon.get(position), position));
                                        }
                                        if (mapTiles.tiles.size() >= 16) {
                                            connection.sendTCP(mapTiles);
                                            mapTiles.tiles.clear();
                                        }
                                    }
                                }
                                mapTiles.dayTimer = CycleDayNight.dayTimer;
                                mapTiles.timeOfDay = game.map.timeOfDay;
                                connection.sendTCP(mapTiles);
                                for (Player otherPlayer : game.players.values()) {
                                    if (otherPlayer != player2) {
                                        if (player2.network.loadingZone.contains(otherPlayer.position)) {
                                            connection.sendTCP(new Network.ServerPlayerData(otherPlayer));
                                            game.server.sendToTCP(otherPlayer.network.connectionId, new Network.ServerPlayerData(player2));
                                        }
                                    }
                                }
                            } else if (object instanceof Network.Logout) {
                                Network.Logout logout = (Network.Logout) object;
                                if (!game.players.containsKey(logout.playerId)) {
                                    System.out.println("Logout: Invalid player ID " + logout.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player3 = game.players.get(logout.playerId);
                                for (Player otherPlayer2 : game.players.values()) {
                                    if (otherPlayer2 != player3) {
                                        if (otherPlayer2.network.loadingZone.contains(player3.position)) {
                                            game.server.sendToTCP(otherPlayer2.network.connectionId, new Network.Logout(player3.network.number));
                                        }
                                    }
                                }
                            } else if (object instanceof Network.MovePlayer) {
                                Network.MovePlayer movePlayer = (Network.MovePlayer) object;
                                if (!game.players.containsKey(movePlayer.playerId)) {
                                    System.out.println("MovePlayer: Invalid player ID " + movePlayer.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player4 = game.players.get(movePlayer.playerId);
                                player4.network.shouldMove = true;
                                player4.network.dirFacing = movePlayer.dirFacing;
                                player4.network.isRunning = movePlayer.isRunning;
                                Vector2 nextPos = player4.position.cpy();
                                if (movePlayer.dirFacing.equals("up")) {
                                    nextPos.add(0.0f, 16.0f);
                                } else if (movePlayer.dirFacing.equals("down")) {
                                    nextPos.add(0.0f, -16.0f);
                                } else if (movePlayer.dirFacing.equals("right")) {
                                    nextPos.add(16.0f, 0.0f);
                                } else if (movePlayer.dirFacing.equals("left")) {
                                    nextPos.add(-16.0f, 0.0f);
                                }
                                for (Player otherPlayer3 : game.players.values()) {
                                    if (otherPlayer3 != player4) {
                                        if (otherPlayer3.network.loadingZone.contains(nextPos)) {
                                            if (!otherPlayer3.network.loadingZone.contains(player4.position)) {
                                                game.server.sendToTCP(otherPlayer3.network.connectionId, new Network.ServerPlayerData(player4));
                                                game.server.sendToTCP(player4.network.connectionId, new Network.ServerPlayerData(otherPlayer3));
                                            }
                                            game.server.sendToTCP(otherPlayer3.network.connectionId, new Network.MovePlayer(player4.network.number, player4.network.dirFacing, player4.network.isRunning));
                                        }
                                    }
                                }
                            } else if (object instanceof Network.DoBattleAction) {
                                Network.DoBattleAction battleAction = (Network.DoBattleAction) object;
                                if (!game.players.containsKey(battleAction.playerId)) {
                                    System.out.println("DoAttack: Invalid player id " + battleAction.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player5 = game.players.get(battleAction.playerId);
                                if (!game.battles.containsKey(battleAction.playerId)) {
                                    System.out.println("DoAttack: player ID not currently in battle " + battleAction.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Battle battle = game.battles.get(battleAction.playerId);
                                Network.BattleTurnData turnData = new Network.BattleTurnData();
                                if (battleAction.type == Battle.DoTurn.Type.SWITCH) {
                                    turnData.oppFirst = false;
                                    player5.numFlees = 0;
                                    player5.currPokemon = player5.pokemon.get(battleAction.pokemonIndex);
                                } else if (battleAction.type == Battle.DoTurn.Type.ITEM) {
                                    turnData.oppFirst = false;
                                    player5.numFlees = 0;
                                    turnData.itemName = battleAction.itemName;
                                    player5.setItemAmount(turnData.itemName, player5.getItemAmount(turnData.itemName).intValue() - 1);
                                    if (player5.getItemAmount(turnData.itemName).intValue() <= 0) {
                                        player5.removeItem(turnData.itemName);
                                    }
                                    if (turnData.itemName.contains("ball")) {
                                        turnData.numWobbles = Battle.gen2CalcIfCaught(game, battle.oppPokemon, battleAction.itemName);
                                        if (turnData.numWobbles == -1) {
                                            player5.pokemon.add(battle.oppPokemon);
                                            Route route = game.map.tiles.get(player5.position).routeBelongsTo;
                                        }
                                    }
                                } else if (battleAction.type == Battle.DoTurn.Type.RUN) {
                                    turnData.oppFirst = false;
                                    turnData.runSuccessful = battle.calcIfRunSuccessful(game, player5);
                                    if (turnData.runSuccessful) {
                                        player5.numFlees = 0;
                                    } else {
                                        player5.numFlees++;
                                    }
                                } else {
                                    player5.numFlees = 0;
                                    boolean found = false;
                                    int i = 0;
                                    while (true) {
                                        if (i >= player5.currPokemon.attacks.length) {
                                            break;
                                        }
                                        if (player5.currPokemon.attacks[i] != null && player5.currPokemon.attacks[i].toLowerCase(Locale.ROOT).equals(battleAction.attack.toLowerCase(Locale.ROOT))) {
                                            found = true;
                                            break;
                                        }
                                        i++;
                                    }
                                    if (!found) {
                                        System.out.println("DoAttack: Invalid attack choice " + battleAction.attack + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                        throw new Exception();
                                    }
                                    turnData.playerAttack = battle.attacks.get(battleAction.attack.toLowerCase(Locale.ROOT));
                                    int yourSpeed = player5.currPokemon.currentStats.get("speed").intValue();
                                    int oppSpeed = battle.oppPokemon.currentStats.get("speed").intValue();
                                    if (yourSpeed > oppSpeed) {
                                        turnData.oppFirst = false;
                                    } else if (yourSpeed < oppSpeed) {
                                        turnData.oppFirst = true;
                                    } else if (game.map.rand.nextInt(2) == 0) {
                                        turnData.oppFirst = true;
                                    }
                                }
                                if (battle.network.expectPlayerSwitch) {
                                    if (battleAction.type == Battle.DoTurn.Type.SWITCH) {
                                        battle.network.expectPlayerSwitch = false;
                                    }
                                    System.out.println("battle.network.expectPlayerSwitch check");
                                    throw new Exception();
                                }
                                String attackChoice = battle.oppPokemon.attacks[game.map.rand.nextInt(battle.oppPokemon.attacks.length)];
                                if (attackChoice == null) {
                                    attackChoice = "Struggle";
                                }
                                turnData.enemyAttack = battle.attacks.get(attackChoice.toLowerCase(Locale.ROOT));
                                if (!turnData.oppFirst) {
                                    int finalHealth = battle.oppPokemon.currentStats.get("hp").intValue();
                                    if (battleAction.type == Battle.DoTurn.Type.ATTACK) {
                                        int damage = Battle.gen2CalcDamage(player5.currPokemon, turnData.playerAttack, battle.oppPokemon);
                                        int currHealth = battle.oppPokemon.currentStats.get("hp").intValue();
                                        finalHealth = currHealth - damage > 0 ? currHealth - damage : 0;
                                        battle.oppPokemon.currentStats.put("hp", Integer.valueOf(finalHealth));
                                        turnData.playerAttack.damage = damage;
                                        if (finalHealth <= 0) {
                                            Route route2 = game.map.tiles.get(player5.position).routeBelongsTo;
                                            player5.currPokemon.exp += battle.calcFaintExp(1);
                                            while (player5.currPokemon.level < 100 && player5.currPokemon.calcExpForLevel(player5.currPokemon.level + 1) <= player5.currPokemon.exp) {
                                                player5.currPokemon.level++;
                                            }
                                            int i2 = 1;
                                            while (true) {
                                                if (i2 > player5.currPokemon.level) {
                                                    break;
                                                } else if (Specie.gen2Evos.get(player5.currPokemon.specie.name.toLowerCase(Locale.ROOT)).containsKey(String.valueOf(i2))) {
                                                    player5.currPokemon.evolveTo(Specie.gen2Evos.get(player5.currPokemon.specie.name.toLowerCase(Locale.ROOT)).get(String.valueOf(i2)));
                                                    break;
                                                } else {
                                                    i2++;
                                                }
                                            }
                                        }
                                    }
                                    if ((battleAction.type == Battle.DoTurn.Type.ATTACK && finalHealth > 0) || ((battleAction.type == Battle.DoTurn.Type.RUN && !turnData.runSuccessful) || (battleAction.type == Battle.DoTurn.Type.ITEM && turnData.numWobbles != -1))) {
                                        int damage2 = Battle.gen2CalcDamage(battle.oppPokemon, turnData.enemyAttack, player5.currPokemon);
                                        int currHealth2 = player5.currPokemon.currentStats.get("hp").intValue();
                                        int finalHealth2 = currHealth2 - damage2 > 0 ? currHealth2 - damage2 : 0;
                                        player5.currPokemon.currentStats.put("hp", Integer.valueOf(finalHealth2));
                                        turnData.enemyAttack.damage = damage2;
                                        if (finalHealth2 <= 0) {
                                            battle.network.expectPlayerSwitch = true;
                                        }
                                    }
                                } else {
                                    int damage3 = Battle.gen2CalcDamage(battle.oppPokemon, turnData.enemyAttack, player5.currPokemon);
                                    int currHealth3 = player5.currPokemon.currentStats.get("hp").intValue();
                                    int finalHealth3 = currHealth3 - damage3 > 0 ? currHealth3 - damage3 : 0;
                                    player5.currPokemon.currentStats.put("hp", Integer.valueOf(finalHealth3));
                                    turnData.enemyAttack.damage = damage3;
                                    if (finalHealth3 > 0) {
                                        int damage4 = Battle.gen2CalcDamage(player5.currPokemon, turnData.playerAttack, battle.oppPokemon);
                                        int currHealth4 = battle.oppPokemon.currentStats.get("hp").intValue();
                                        int finalHealth4 = currHealth4 - damage4 > 0 ? currHealth4 - damage4 : 0;
                                        battle.oppPokemon.currentStats.put("hp", Integer.valueOf(finalHealth4));
                                        turnData.playerAttack.damage = damage4;
                                        if (finalHealth4 <= 0) {
                                            Route route3 = game.map.tiles.get(player5.position).routeBelongsTo;
                                            player5.currPokemon.exp += battle.calcFaintExp(1);
                                            while (player5.currPokemon.level < 100 && player5.currPokemon.calcExpForLevel(player5.currPokemon.level + 1) <= player5.currPokemon.exp) {
                                                player5.currPokemon.level++;
                                            }
                                            int i3 = 1;
                                            while (true) {
                                                if (i3 > player5.currPokemon.level) {
                                                    break;
                                                } else if (Specie.gen2Evos.get(player5.currPokemon.specie.name.toLowerCase(Locale.ROOT)).containsKey(String.valueOf(i3))) {
                                                    player5.currPokemon.evolveTo(Specie.gen2Evos.get(player5.currPokemon.specie.name.toLowerCase(Locale.ROOT)).get(String.valueOf(i3)));
                                                    break;
                                                } else {
                                                    i3++;
                                                }
                                            }
                                        }
                                    } else {
                                        battle.network.expectPlayerSwitch = true;
                                    }
                                }
                                if (battle.oppPokemon.currentStats.get("hp").intValue() <= 0 || ((battleAction.type == Battle.DoTurn.Type.RUN && turnData.runSuccessful) || (battleAction.type == Battle.DoTurn.Type.ITEM && turnData.numWobbles == -1))) {
                                    battle.oppPokemon.inBattle = false;
                                    player5.canMove = true;
                                    player5.numFlees = 0;
                                    game.battles.remove(player5.network.id);
                                }
                                boolean hasAlivePokemon = false;
                                Iterator<Pokemon> it2 = player5.pokemon.iterator();
                                while (true) {
                                    if (!it2.hasNext()) {
                                        break;
                                    } else if (it2.next().currentStats.get("hp").intValue() > 0) {
                                        hasAlivePokemon = true;
                                        break;
                                    }
                                }
                                if (!hasAlivePokemon) {
                                    battle.oppPokemon.inBattle = false;
                                    player5.canMove = true;
                                    player5.numFlees = 0;
                                    game.battles.remove(player5.network.id);
                                    player5.position.set(player5.spawnLoc);
                                    Iterator<Pokemon> it3 = player5.pokemon.iterator();
                                    while (it3.hasNext()) {
                                        Pokemon pokemon = it3.next();
                                        pokemon.currentStats.put("hp", Integer.valueOf(pokemon.maxStats.get("hp").intValue() / 2));
                                    }
                                    for (Player otherPlayer4 : game.players.values()) {
                                        if (otherPlayer4 != player5) {
                                            if (otherPlayer4.network.loadingZone.contains(player5.position)) {
                                                game.server.sendToTCP(otherPlayer4.network.connectionId, new Network.ServerPlayerData(player5));
                                            }
                                        }
                                    }
                                }
                                System.out.println("Sending turn data.");
                                game.server.sendToTCP(player5.network.connectionId, turnData);
                            } else if (object instanceof Network.TileData) {
                                Network.TileData tileData = (Network.TileData) object;
                                if (!game.map.tiles.containsKey(tileData.pos)) {
                                    System.out.println("TileData: Invalid tile position " + tileData.pos.toString() + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                } else if (game.map.tiles.get(tileData.pos).isSolid) {
                                    throw new Exception();
                                } else {
                                    for (Player player6 : game.players.values()) {
                                        if (player6.position.equals(tileData.pos)) {
                                            throw new Exception();
                                        }
                                    }
                                    Tile newTile = new Tile(tileData.tileName, tileData.tileNameUpper, tileData.pos.cpy(), true, game.map.tiles.get(tileData.pos).routeBelongsTo);
                                    newTile.hasItem = tileData.hasItem;
                                    newTile.hasItemAmount = tileData.hasItemAmount;
                                    game.map.tiles.put(tileData.pos.cpy(), newTile);
                                    game.map.adjustSurroundingTiles(newTile);
                                    for (Player player7 : game.players.values()) {
                                        if (player7.network.loadingZone.contains(tileData.pos)) {
                                            game.server.sendToTCP(player7.network.connectionId, tileData);
                                        }
                                    }
                                }
                            } else if (object instanceof Network.UseHM) {
                                Network.UseHM useHM = (Network.UseHM) object;
                                if (!game.players.containsKey(useHM.playerId)) {
                                    System.out.println("UseHM: Invalid player id " + useHM.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player8 = game.players.get(useHM.playerId);
                                if (!player8.pokemon.get(useHM.pokemonIndex).hms.contains(useHM.hm)) {
                                    System.out.println("UseHM: Invalid HM " + useHM.hm + " for index " + useHM.pokemonIndex + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                }
                                if (useHM.hm.equals("STOP")) {
                                    player8.currFieldMove = "";
                                } else if (useHM.hm.equals("SWITCH")) {
                                    Pokemon movePokemon = player8.pokemon.get(useHM.pokemonIndex);
                                    Pokemon movePokemon2 = player8.pokemon.get(useHM.movePos);
                                    player8.pokemon.remove(useHM.pokemonIndex);
                                    player8.pokemon.add(useHM.pokemonIndex, movePokemon2);
                                    player8.pokemon.remove(useHM.movePos);
                                    player8.pokemon.add(useHM.movePos, movePokemon);
                                    if (useHM.pokemonIndex == 0) {
                                        player8.currPokemon = movePokemon2;
                                    }
                                    if (useHM.movePos == 0) {
                                        player8.currPokemon = movePokemon;
                                    }
                                } else if (!useHM.hm.equals("BUILD")) {
                                    if (useHM.hm.equals("CUT")) {
                                        Vector2 pos = player8.facingPos(useHM.dirFacing);
                                        Tile currTile = player8.network.tiles.get(pos);
                                        if (currTile.isCuttable) {
                                            Action action = new CutTreeAnim(game, game.map.overworldTiles.get(pos), null);
                                            game.map.interiorTiles.get(game.map.interiorTilesIndex).remove(currTile.position.cpy());
                                            if (!currTile.items().isEmpty()) {
                                                for (String item : currTile.items().keySet()) {
                                                    if (player8.hasItem(item)) {
                                                        player8.setItemAmount(item, player8.getItemAmount(item).intValue() + currTile.items().get(item).intValue());
                                                    } else {
                                                        player8.setItemAmount(item, currTile.items().get(item).intValue());
                                                    }
                                                }
                                                currTile.items().clear();
                                            }
                                            game.insertAction(action);
                                        }
                                    } else if (useHM.hm.equals("HEADBUTT")) {
                                        player8.currFieldMove = "HEADBUTT";
                                    } else if (useHM.hm.equals("JUMP")) {
                                        player8.currFieldMove = "RIDE";
                                    }
                                }
                                for (Player otherPlayer5 : game.players.values()) {
                                    if (player8 != otherPlayer5) {
                                        if (otherPlayer5.network.loadingZone.contains(player8.position)) {
                                            useHM.playerId = player8.network.number;
                                            game.server.sendToTCP(otherPlayer5.network.connectionId, useHM);
                                        }
                                    }
                                }
                            } else if (object instanceof Network.UseItem) {
                                Network.UseItem useItem = (Network.UseItem) object;
                                if (!game.players.containsKey(useItem.playerId)) {
                                    System.out.println("UseItem: Invalid player id " + useItem.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player9 = game.players.get(useItem.playerId);
                                if (!player9.hasItem(useItem.item)) {
                                    System.out.println("UseItem: None of this item in inventory: " + useItem.item + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Vector2 pos2 = player9.facingPos(useItem.dirFacing);
                                if (useItem.item.contains("apricorn")) {
                                    game.insertAction(new PlantTree(pos2, null));
                                }
                                player9.setItemAmount(useItem.item, player9.getItemAmount(useItem.item).intValue() - 1);
                                if (player9.getItemAmount(useItem.item).intValue() <= 0) {
                                    player9.removeItem(useItem.item);
                                }
                                for (Player otherPlayer6 : game.players.values()) {
                                    if (player9 != otherPlayer6) {
                                        if (otherPlayer6.network.loadingZone.contains(player9.position)) {
                                            useItem.playerId = player9.network.number;
                                            game.server.sendToTCP(otherPlayer6.network.connectionId, useItem);
                                        }
                                    }
                                }
                            } else if (object instanceof Network.Sleep) {
                                Network.Sleep sleep = (Network.Sleep) object;
                                if (!game.players.containsKey(sleep.playerId)) {
                                    System.out.println("UseHM: Invalid player id " + sleep.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player10 = game.players.get(sleep.playerId);
                                player10.isSleeping = sleep.isSleeping;
                                player10.canMove = !sleep.isSleeping;
                                if (!player10.isSleeping) {
                                    player10.spawnLoc = player10.position.cpy();
                                    int i4 = 0;
                                    Iterator<Pokemon> it4 = player10.pokemon.iterator();
                                    while (it4.hasNext()) {
                                        connection.sendTCP(new Network.PokemonData(it4.next(), i4));
                                        i4++;
                                    }
                                }
                            } else if (object instanceof Network.Craft) {
                                Network.Craft craft = (Network.Craft) object;
                                if (!game.players.containsKey(craft.playerId)) {
                                    System.out.println("Craft: Invalid player id " + craft.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player11 = game.players.get(craft.playerId);
                                if (player11.hasCraftRequirements(Player.crafts, craft.craftIndex, craft.amount)) {
                                    player11.craftItem(Player.crafts, craft.craftIndex, craft.amount);
                                }
                            } else if (object instanceof Network.DropItem) {
                                Network.DropItem dropItem = (Network.DropItem) object;
                                if (!game.players.containsKey(dropItem.playerId)) {
                                    System.out.println("DropItem: Invalid player id " + dropItem.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player12 = game.players.get(dropItem.playerId);
                                player12.setItemAmount(dropItem.itemName, player12.getItemAmount(dropItem.itemName).intValue() - dropItem.amount);
                                if (player12.getItemAmount(dropItem.itemName).intValue() <= 0) {
                                    player12.removeItem(dropItem.itemName);
                                }
                            } else if (object instanceof Network.DropPokemon) {
                                Network.DropPokemon dropPokemon = (Network.DropPokemon) object;
                                if (!game.players.containsKey(dropPokemon.playerId)) {
                                    System.out.println("DropPokemon: Invalid player id " + dropPokemon.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player13 = game.players.get(dropPokemon.playerId);
                                if (dropPokemon.pickingUp) {
                                    Vector2 pos3 = dropPokemon.pos;
                                    Pokemon pokemon2 = game.map.pokemon.get(pos3);
                                    if (player13.pokemon.size() < 6 && pokemon2 != null && player13.network.loadingZone.contains(pos3)) {
                                        player13.pokemon.add(pokemon2);
                                        game.map.pokemon.remove(pos3);
                                        game.actionStack.remove(pokemon2.standingAction);
                                    }
                                    for (Player otherPlayer7 : game.players.values()) {
                                        if (otherPlayer7 != player13) {
                                            if (player13.network.loadingZone.contains(pos3)) {
                                                game.server.sendToTCP(otherPlayer7.network.connectionId, new Network.OverworldPokemonData(pokemon2, pos3, true));
                                            }
                                        }
                                    }
                                } else {
                                    Vector2 pos4 = player13.facingPos(dropPokemon.dirFacing);
                                    Pokemon pokemon3 = player13.pokemon.get(dropPokemon.index);
                                    pokemon3.position = pos4;
                                    player13.pokemon.remove(dropPokemon.index);
                                    if (dropPokemon.index == 0) {
                                        Iterator<Pokemon> it5 = player13.pokemon.iterator();
                                        while (true) {
                                            if (!it5.hasNext()) {
                                                break;
                                            }
                                            Pokemon currPokemon = it5.next();
                                            if (currPokemon.currentStats.get("hp").intValue() > 0) {
                                                player13.currPokemon = currPokemon;
                                                break;
                                            }
                                        }
                                    }
                                    pokemon3.mapTiles = player13.network.tiles;
                                    pokemon3.canMove = true;
                                    Game game2 = game;
                                    Objects.requireNonNull(pokemon3);
                                    game2.insertAction(new Pokemon.Standing());
                                    for (Player otherPlayer8 : game.players.values()) {
                                        if (otherPlayer8 != player13) {
                                            if (player13.network.loadingZone.contains(pos4)) {
                                                game.server.sendToTCP(otherPlayer8.network.connectionId, new Network.OverworldPokemonData(pokemon3, pos4));
                                            }
                                        }
                                    }
                                }
                            } else if (object instanceof Network.PausePokemon) {
                                Network.PausePokemon pausePokemon = (Network.PausePokemon) object;
                                if (!game.players.containsKey(pausePokemon.playerId)) {
                                    System.out.println("DropPokemon: Invalid player id " + pausePokemon.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                if (game.players.get(pausePokemon.playerId).network.loadingZone.contains(pausePokemon.position) && game.map.pokemon.containsKey(pausePokemon.position)) {
                                    game.map.pokemon.get(pausePokemon.position).canMove = !pausePokemon.shouldPause;
                                }
                                if (pausePokemon.shouldPause) {
                                    connection.sendTCP(pausePokemon);
                                }
                            } else if (object instanceof Network.PickupItem) {
                                Network.PickupItem pickupItem = (Network.PickupItem) object;
                                if (!game.players.containsKey(pickupItem.playerId)) {
                                    System.out.println("PickupItem: Invalid player id " + pickupItem.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Player player14 = game.players.get(pickupItem.playerId);
                                Vector2 pos5 = new Vector2(0.0f, 0.0f);
                                if (pickupItem.dirFacing.equals("right")) {
                                    pos5 = new Vector2(player14.position.cpy().add(16.0f, 0.0f));
                                } else if (pickupItem.dirFacing.equals("left")) {
                                    pos5 = new Vector2(player14.position.cpy().add(-16.0f, 0.0f));
                                } else if (pickupItem.dirFacing.equals("up")) {
                                    pos5 = new Vector2(player14.position.cpy().add(0.0f, 16.0f));
                                } else if (pickupItem.dirFacing.equals("down")) {
                                    pos5 = new Vector2(player14.position.cpy().add(0.0f, -16.0f));
                                }
                                Tile tile3 = game.map.tiles.get(pos5);
                                if (!(tile3 == null || tile3.hasItem == null)) {
                                    tile3.pickUpItem(player14);
                                    pickupItem.pos = pos5;
                                    connection.sendTCP(pickupItem);
                                    Network.TileData tileData2 = new Network.TileData(tile3);
                                    for (Player otherPlayer9 : game.players.values()) {
                                        if (player14 != otherPlayer9) {
                                            if (player14.network.loadingZone.contains(tileData2.pos)) {
                                                game.server.sendToTCP(player14.network.connectionId, tileData2);
                                            }
                                        }
                                    }
                                }
                            } else if (object instanceof Network.LearnMove) {
                                Network.LearnMove learnMove = (Network.LearnMove) object;
                                if (!game.players.containsKey(learnMove.playerId)) {
                                    System.out.println("DropItem: Invalid player id " + learnMove.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                }
                                Pokemon pokemon4 = game.players.get(learnMove.playerId).pokemon.get(learnMove.pokemonIndex);
                                learnMove.moveName = learnMove.moveName.toLowerCase(Locale.ROOT);
                                boolean foundMove = false;
                                for (int i5 = 0; i5 <= pokemon4.level; i5++) {
                                    if (pokemon4.learnSet.containsKey(Integer.valueOf(i5))) {
                                        String[] strArr = pokemon4.learnSet.get(Integer.valueOf(i5));
                                        int length = strArr.length;
                                        int i6 = 0;
                                        while (true) {
                                            if (i6 >= length) {
                                                break;
                                            } else if (strArr[i6].toLowerCase(Locale.ROOT).equals(learnMove.moveName)) {
                                                foundMove = true;
                                                break;
                                            } else {
                                                i6++;
                                            }
                                        }
                                        if (foundMove) {
                                            break;
                                        }
                                    }
                                }
                                if (foundMove) {
                                    pokemon4.attacks[learnMove.replaceIndex] = learnMove.moveName;
                                }
                            } else if (object instanceof Network.BattleData) {
                                Network.BattleData battleData = (Network.BattleData) object;
                                if (!battleData.pokemonData.name.equals("ghost")) {
                                    System.out.println("BattleData: Invalid encounter for " + battleData.pokemonData.name + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                } else if (!game.players.containsKey(battleData.playerId)) {
                                    System.out.println("BattleData: Invalid player id " + battleData.playerId + ", sent by: " + connection.getRemoteAddressTCP().toString());
                                    throw new Exception();
                                } else {
                                    Player player15 = game.players.get(battleData.playerId);
                                    player15.setCurrPokemon();
                                    player15.canMove = false;
                                    game.battles.put(player15.network.id, new Battle());
                                    game.battles.get(player15.network.id).oppPokemon = new Pokemon(battleData.pokemonData);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        synchronized (this) {
                            notify();
                        }
                    }
                };
                Gdx.app.postRunnable(runnable);
                try {
                    synchronized (runnable) {
                        runnable.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override // com.corona.game.Action
    public Action.Layer getLayer() {
        return this.layer;
    }
}
